package com.byzk.p2p.admin.group.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.group.dto.Group;
import com.byzk.p2p.admin.group.service.GroupService;
import com.byzk.p2p.admin.system.dto.GroupRefMenu;
import com.byzk.p2p.admin.system.service.MenuService;
import com.byzk.p2p.admin.system.vo.MenuVo;

@Controller
@RequestMapping(value = "/admin/group")
public class GroupController extends AdminBaseController {

	@Autowired
	private GroupService groupService;
	@Autowired
	private MenuService menuService;
	
	@RequestMapping(value="/list")
	public String listGroup(Model model,
			@RequestParam(value="index",required = false,defaultValue="1") int index,
			@RequestParam(value="limit",required = false,defaultValue="10") int limit){
		PageInfo<Group> pageInfo = groupService.groupList(null,index, limit);
		model.addAttribute("pageInfo", pageInfo);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	@RequestMapping(value="/saveGroup")
	public void saveGroup(@RequestBody Group group) {
		try {
			if (StringUtils.isBlank(group.getName())) {
				printJSON(new JsonMsg(1, "名称不能为空"));
				return;
			}
			if (StringUtils.isBlank(group.getFlag())) {
				printJSON(new JsonMsg(2, "组标识不能为空"));	
				return;
			}
			if (group.getId() == null) {
				group.setCreateTime(new Date());
			}
			Integer i = groupService.saveOrUpdateGroup(group);
			if (i > 0) {
				printJSON(new JsonMsg(-1, "操作成功"));
			}else{
				printJSON(new JsonMsg(0, "操作失败"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="deleteGroup")
	public String deleteGroup(Model model,Integer id){
		//先删除组再删除组下面关联的菜单
		groupService.deleteGroup(id);
		menuService.deleteGroupRefMenu(id);
		return ftl(Thread.currentThread().getStackTrace()); 
	}
	
	@RequestMapping(value="/settingGroup")
	public String settingGroup(Model model,@RequestParam(value="id",required = false ,defaultValue = "0") int id){
		Group group = groupService.getGroupById(id);
		model.addAttribute("item", group);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	
	@RequestMapping(value="/settingGroupRefMenu")
	public String settingGroupRefMenu(Model model,@RequestParam(value="id") int id){
		model.addAttribute("groupId", id);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	@RequestMapping(value="/saveGroupMenu")
	public void saveGroupMenu(@RequestBody List<JSONObject> groupRefMenus) {
		try {
			if (CollectionUtils.isNotEmpty(groupRefMenus)) {
				List<GroupRefMenu> list = new ArrayList<GroupRefMenu>();
				Integer groupId = groupRefMenus.get(0).getInteger("groupId");
				//先删除组下的所有关联菜单
				menuService.deleteGroupRefMenu(groupId);
				for (JSONObject jsonObject : groupRefMenus) {
					Integer menuId = jsonObject.getInteger("menuId");
					GroupRefMenu refMenu = new GroupRefMenu();
					refMenu.setGroupId(groupId);
					refMenu.setMenuId(menuId);
					refMenu.setCreateTime(new Date());
					list.add(refMenu);
				}
				Integer i = menuService.insertGroupRefMenu(list);
				if (i >0) {
					printJSON(new JsonMsg(-1, "操作成功"));
				}else{
					printJSON(new JsonMsg(0, "操作失败"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	 
	@RequestMapping(value="/data")
	public void getGroupRefMenu(@RequestParam Integer groupId) {
		//获取所有的菜单
		List<MenuVo> menus = menuService.getAllMenu();
		List<GroupRefMenu> groupRefMenus = menuService.getGroupRefMenu(groupId);
		JSONArray jsonArray = new JSONArray();
		if (CollectionUtils.isNotEmpty(menus)) {
			for (MenuVo menu : menus) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("id", menu.getId());
				jsonObject.put("name", menu.getName());
				jsonObject.put("pId", menu.getPid());
				jsonObject.put("open", true);
				//存在组与菜单的关系，需要默认选中
				if(CollectionUtils.isNotEmpty(groupRefMenus)){
					for (GroupRefMenu groupRefMenu : groupRefMenus) {
						if(menu.getId().equals(groupRefMenu.getMenuId())){
							jsonObject.put("checked", true);
							continue;
						}
					}
				}else{
					jsonObject.put("checked", false);
				}
				jsonArray.add(jsonObject);
			}
		}
		try {
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter pw = response.getWriter();
			pw.print(jsonArray.toJSONString());
//			System.out.println("jsonArray.toJSONString() = " +jsonArray.toJSONString());
			pw.flush();
			pw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
